package app.imttf.offline.model;

import app.imttf.offline.model.common.OfflineTtfAM;
import app.imttf.offline.model.view.buatttf.BpbPerFakturViewImpl;
import app.imttf.offline.model.view.buatttf.InfoSupplierCheckViewImpl;
import app.imttf.offline.model.view.buatttf.InfoSupplierScanViewImpl;
import app.imttf.offline.model.view.buatttf.NumberTtfHeaderViewImpl;
import app.imttf.offline.model.view.buatttf.NumberTtfHeaderViewRowImpl;
import app.imttf.offline.model.view.buatttf.TtfFpTotalAmountViewImpl;
import app.imttf.offline.model.view.buatttf.TtfFpTotalAmountViewRowImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderGetKeyViewImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderTotalAmountViewImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderTotalAmountViewRowImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderUpdateStatusViewImpl;
import app.imttf.offline.model.view.buatttf.TtfHeaderViewImpl;
import app.imttf.offline.model.view.validasi.CheckValLevel1CompleteViewImpl;
import app.imttf.offline.model.view.validasi.CheckValLevel2CompleteViewImpl;
import app.imttf.offline.model.view.validasi.NilaiToleransiDppViewImpl;
import app.imttf.offline.model.view.validasi.NilaiToleransiDppViewRowImpl;
import app.imttf.offline.model.view.validasi.NilaiToleransiPpnViewImpl;
import app.imttf.offline.model.view.validasi.NilaiToleransiPpnViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfBpbUpdateValidateLvl2ViewImpl;
import app.imttf.offline.model.view.validasi.TtfBpbUpdateValidateLvl2ViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfBpbUpdateValidateViewImpl;
import app.imttf.offline.model.view.validasi.TtfBpbUpdateValidateViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfBpbValidationViewImpl;
import app.imttf.offline.model.view.validasi.TtfBpbValidationViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfFpUpdateDataViewImpl;
import app.imttf.offline.model.view.validasi.TtfFpUpdateValidateLvl2ViewImpl;
import app.imttf.offline.model.view.validasi.TtfFpUpdateValidateLvl2ViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfFpUpdateValidateViewImpl;
import app.imttf.offline.model.view.validasi.TtfFpUpdateValidateViewRowImpl;
import app.imttf.offline.model.view.validasi.TtfFpValidationViewImpl;
import app.imttf.offline.model.view.validasi.TtfFpValidationViewRowImpl;
import app.imttf.offline.model.view.validasi.ValidasiNilaiDppViewImpl;
import app.imttf.offline.model.view.validasi.ValidasiNilaiDppViewRowImpl;
import app.imttf.offline.model.view.validasi.ValidasiNilaiTaxViewImpl;
import app.imttf.offline.model.view.validasi.ValidasiNilaiTaxViewRowImpl;
import app.imttf.offline.model.view.validasi.ValidasiNoBpbViewImpl;
import app.imttf.offline.model.view.validasi.ValidasiNpwpIndomarcoViewImpl;

import app.imttf.offline.model.view.validasi.ValidasiNpwpSupplierViewImpl;

import app.imttf.offline.model.view.validasi.ValidasiTglBpbViewImpl;

import app.imttf.offline.model.view.validasi.ValidasiTglBpbViewRowImpl;

import app.imttf.offline.model.view.validasi.ValidasiTglFpViewImpl;

import java.math.BigDecimal;


import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.text.ParseException;
import java.text.SimpleDateFormat;


import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.sql.DataSource;

import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewObjectImpl;

// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Wed Jan 29 15:40:48 ICT 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class OfflineTtfAMImpl extends ApplicationModuleImpl implements OfflineTtfAM {
    /**
     * This is the default constructor (do not remove).
     */

    private StringBuilder yearNowStr;
    private SimpleDateFormat dateYearFormat = new SimpleDateFormat("yy");

    public OfflineTtfAMImpl() {
    }

    /**
     * Container's getter for InfoSupplierCheckView1.
     * @return InfoSupplierCheckView1
     */
    public InfoSupplierCheckViewImpl getInfoSupplierCheckView1() {
        return (InfoSupplierCheckViewImpl)findViewObject("InfoSupplierCheckView1");
    }

    /**
     * Container's getter for InfoSupplierScanView1.
     * @return InfoSupplierScanView1
     */
    public ViewObjectImpl getInfoSupplierScanView1() {
        return (ViewObjectImpl)findViewObject("InfoSupplierScanView1");
    }

    /**
     * Container's getter for VerifikasiNewTtfPkpView1.
     * @return VerifikasiNewTtfPkpView1
     */
    public ViewObjectImpl getVerifikasiNewTtfPkpView1() {
        return (ViewObjectImpl)findViewObject("VerifikasiNewTtfPkpView1");
    }

    /**
     * Container's getter for VerifikasiNewTtfNonPkpView1.
     * @return VerifikasiNewTtfNonPkpView1
     */
    public ViewObjectImpl getVerifikasiNewTtfNonPkpView1() {
        return (ViewObjectImpl)findViewObject("VerifikasiNewTtfNonPkpView1");
    }

    /**
     * Container's getter for TtfHeaderView1.
     * @return TtfHeaderView1
     */
    public TtfHeaderViewImpl getTtfHeaderView1() {
        return (TtfHeaderViewImpl)findViewObject("TtfHeaderView1");
    }

    public void amountTotalTtfPkp(String keyTtf) {

        BigDecimal ttfAmountTotal = BigDecimal.ZERO;

        TtfFpTotalAmountViewImpl ttfFpTotalAmount =
            this.getTtfFpTotalAmountView1();
        ttfFpTotalAmount.setkeyTtf(keyTtf);
        ttfFpTotalAmount.executeQuery();

        long ttfFpTotalAmountRowCount =
            ttfFpTotalAmount.getEstimatedRowCount();

        if (ttfFpTotalAmountRowCount > 0) {
            for (int i = 0; i < ttfFpTotalAmountRowCount; i++) {
                TtfFpTotalAmountViewRowImpl ttfFpTotalAmountRow;
                ttfFpTotalAmountRow =
                        (TtfFpTotalAmountViewRowImpl)ttfFpTotalAmount.getRowSetIterator().next();

                ttfAmountTotal =
                        ttfAmountTotal.add(new BigDecimal(ttfFpTotalAmountRow.getAmountTotal()));

            }
        }

        // Update TTF Header Amount Total
        TtfHeaderTotalAmountViewImpl ttfHeader =
            this.getTtfHeaderTotalAmountView1();
        ttfHeader.setkeyTtf(keyTtf);
        ttfHeader.executeQuery();

        try {
            TtfHeaderTotalAmountViewRowImpl ttfHeaderTotalAmountRow =
                (TtfHeaderTotalAmountViewRowImpl)ttfHeader.getRowSetIterator().next();
            ttfHeaderTotalAmountRow.setTtfAmount(ttfAmountTotal);
        } finally {
            getDBTransaction().commit();
        }

    }

    public void insertTtfNum(String keyTtf, String kodeBranch) {

        Date dateNow = new Date();
        yearNowStr = new StringBuilder(dateYearFormat.format(dateNow));

        Connection conn = null;
        String ttfNumber = "";
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("jdbc/imttfDS");
            conn = ds.getConnection();
            PreparedStatement statement =
                conn.prepareStatement("SELECT TO_CHAR(SYSDATE, 'YY') || '" +
                                      kodeBranch +
                                      "' || TO_CHAR(MAX(TO_NUMBER(SUBSTR(TTF_NUM,6,10)))+1, " +
                                      "'FM00000' )AS NEW_TTF_NUM " +
                                      "FROM (SELECT TtfHeader.TTF_NUM AS TTF_NUM FROM TTF_HEADER TtfHeader) " +
                                      "WHERE SUBSTR(TTF_NUM,3,3) = '" +
                                      kodeBranch + "' " +
                                      "AND SUBSTR(TTF_NUM, 1,2) = TO_CHAR(SYSDATE, 'YY')");
            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                ttfNumber = rs.getString("NEW_TTF_NUM");
                if (ttfNumber.length() < 10) {
                    ttfNumber = yearNowStr + kodeBranch + "00001";
                }
            }

            statement.close();
            rs.close();
            conn.close();
        } catch (Exception exc) {
            exc.printStackTrace();
        }

        // Update TTF Header Amount Total
        NumberTtfHeaderViewImpl numberTtfHeader =
            this.getNumberTtfHeaderView1();
        numberTtfHeader.setkeyTtf(keyTtf);
        numberTtfHeader.executeQuery();

        try {
            NumberTtfHeaderViewRowImpl numberTtfHeaderRow =
                (NumberTtfHeaderViewRowImpl)numberTtfHeader.getRowSetIterator().next();
            numberTtfHeaderRow.setTtfNum(ttfNumber);
        } finally {
            getDBTransaction().commit();
        }

    }

    /**
     * Container's getter for BpbPerFakturView1.
     * @return BpbPerFakturView1
     */
    public BpbPerFakturViewImpl getBpbPerFakturView1() {
        return (BpbPerFakturViewImpl)findViewObject("BpbPerFakturView1");
    }

    /**
     * Container's getter for TtfFpTotalAmountView1.
     * @return TtfFpTotalAmountView1
     */
    public TtfFpTotalAmountViewImpl getTtfFpTotalAmountView1() {
        return (TtfFpTotalAmountViewImpl)findViewObject("TtfFpTotalAmountView1");
    }

    /**
     * Container's getter for TtfHeaderTotalAmountView1.
     * @return TtfHeaderTotalAmountView1
     */
    public TtfHeaderTotalAmountViewImpl getTtfHeaderTotalAmountView1() {
        return (TtfHeaderTotalAmountViewImpl)findViewObject("TtfHeaderTotalAmountView1");
    }

    /**
     * Container's getter for NumberTtfHeaderView1.
     * @return NumberTtfHeaderView1
     */
    public NumberTtfHeaderViewImpl getNumberTtfHeaderView1() {
        return (NumberTtfHeaderViewImpl)findViewObject("NumberTtfHeaderView1");
    }

    /**
     * Container's getter for TtfHeaderGetKeyView1.
     * @return TtfHeaderGetKeyView1
     */
    public TtfHeaderGetKeyViewImpl getTtfHeaderGetKeyView1() {
        return (TtfHeaderGetKeyViewImpl)findViewObject("TtfHeaderGetKeyView1");
    }

    // Validation Functions

    private void validasiPkpSup(String keyFpBpb, String branchCode,
                                String siteCode, String noPkp) {
        ValidasiNpwpSupplierViewImpl validasiNpwpSupplierView;
        validasiNpwpSupplierView = getValidasiNpwpSupplierView1();
        validasiNpwpSupplierView.setbranchCode(branchCode);
        validasiNpwpSupplierView.setnoPkp(noPkp);
        validasiNpwpSupplierView.setsiteCode(siteCode);
        validasiNpwpSupplierView.executeQuery();

        long npwpSupplierNumRow =
            validasiNpwpSupplierView.getEstimatedRowCount();

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (npwpSupplierNumRow == 0) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpSupVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpSupVal(null);
        }
    }

    private void validasiPkpIndomarco(String keyFpBpb, String branchCode,
                                      String pkpIndomarco) {
        ValidasiNpwpIndomarcoViewImpl validasiNpwpIndomarcoView;
        validasiNpwpIndomarcoView = getValidasiNpwpIndomarcoView1();
        validasiNpwpIndomarcoView.setbranchCode(branchCode);
        validasiNpwpIndomarcoView.setnoPkp(pkpIndomarco);
        validasiNpwpIndomarcoView.executeQuery();

        long npwpIndomarcoNumRow =
            validasiNpwpIndomarcoView.getEstimatedRowCount();

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (npwpIndomarcoNumRow == 0) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpIdmVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setPkpIdmVal(null);
        }
    }

    private void validasiFormatFp(String keyFpBpb, String noFp) {
        Pattern pattern = Pattern.compile("\\d{3}.\\d{3}-\\d{2}.\\d{8}");
        Matcher matcher = pattern.matcher(noFp);

        TtfFpUpdateValidateViewImpl ttfFpUpdateValidateView =
            getTtfFpUpdateValidateView1();
        ttfFpUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateView.executeQuery();

        if (!matcher.matches()) {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setNoFpVal("F");
        } else {
            TtfFpUpdateValidateViewRowImpl ttfFpUpdateValidateRow =
                (TtfFpUpdateValidateViewRowImpl)ttfFpUpdateValidateView.first();
            ttfFpUpdateValidateRow.setNoFpVal(null);
        }
    }

    private void validasiNoBpb(String keyFpBpb, String noBpb, String supCode) {
        ValidasiNoBpbViewImpl validasiNoBpbView;
        validasiNoBpbView = getValidasiNoBpbView1();
        validasiNoBpbView.setnoBpb(noBpb);
        validasiNoBpbView.setsupCode(supCode);
        validasiNoBpbView.executeQuery();

        long validasiNoBpbNumRow = validasiNoBpbView.getEstimatedRowCount();

        TtfBpbUpdateValidateViewImpl ttfBpbUpdateValidateView =
            getTtfBpbUpdateValidateView1();
        ttfBpbUpdateValidateView.setkeyFpBpb(keyFpBpb);
        ttfBpbUpdateValidateView.setnoBpb(noBpb);
        ttfBpbUpdateValidateView.executeQuery();

        if (validasiNoBpbNumRow == 0) {
            TtfBpbUpdateValidateViewRowImpl ttfBpbUpdateValidateRow =
                (TtfBpbUpdateValidateViewRowImpl)ttfBpbUpdateValidateView.first();
            ttfBpbUpdateValidateRow.setInvoiceNumVal("F");
        } else {
            TtfBpbUpdateValidateViewRowImpl ttfBpbUpdateValidateRow =
                (TtfBpbUpdateValidateViewRowImpl)ttfBpbUpdateValidateView.first();
            ttfBpbUpdateValidateRow.setInvoiceNumVal(null);
        }
    }

    private void validasiTglBpb(String keyTtf, String noBpb) {

        // SYARAT VALIDASI LVL2:
        // 1. Syarat No BPB sudah pasti ada di list tabel master BPB
        Date tglNow = new Date();
        Date tglBpbMax;

        ValidasiTglBpbViewImpl validasiTglBpbView;
        validasiTglBpbView = getValidasiTglBpbView1();
        validasiTglBpbView.setkeyTtf(keyTtf);
        validasiTglBpbView.setnoBpb(noBpb);
        validasiTglBpbView.executeQuery();

        long validasiTglBpbNumRow = validasiTglBpbView.getEstimatedRowCount();

        if (validasiTglBpbNumRow > 0) {
            ValidasiTglBpbViewRowImpl validasiTglBpbRow =
                (ValidasiTglBpbViewRowImpl)validasiTglBpbView.first();
            String tglBpbMaxStr = validasiTglBpbRow.getTglBpbMax().toString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


            try {
                tglBpbMax = sdf.parse(tglBpbMaxStr);
            } catch (ParseException e) {
                System.out.println("ERROR: Date Conversion.");
                tglBpbMax = new Date();
            }

            TtfBpbUpdateValidateLvl2ViewImpl ttfBpbUpdateValidateLvl2View =
                getTtfBpbUpdateValidateLvl2View1();
            ttfBpbUpdateValidateLvl2View.setkeyTtf(keyTtf);
            ttfBpbUpdateValidateLvl2View.setnoBpb(noBpb);
            ttfBpbUpdateValidateLvl2View.executeQuery();
            
            if (tglNow.compareTo(tglBpbMax) > 0) {
                TtfBpbUpdateValidateLvl2ViewRowImpl ttfBpbUpdateValidateLvl2Row =
                    (TtfBpbUpdateValidateLvl2ViewRowImpl)ttfBpbUpdateValidateLvl2View.first();
                ttfBpbUpdateValidateLvl2Row.setTglBpbVal("F");
            } else {
                TtfBpbUpdateValidateLvl2ViewRowImpl ttfBpbUpdateValidateLvl2Row =
                    (TtfBpbUpdateValidateLvl2ViewRowImpl)ttfBpbUpdateValidateLvl2View.first();
                ttfBpbUpdateValidateLvl2Row.setTglBpbVal(null);
            }

        }

    }

    private void validasiTglFp(String keyFpBpb, Number amtTax) {

        ValidasiTglFpViewImpl validasiTglFpView;
        validasiTglFpView = getValidasiTglFpView1();
        validasiTglFpView.setkeyFpBpb(keyFpBpb);
        validasiTglFpView.executeQuery();

        long validasiTglFpNumRow = validasiTglFpView.getEstimatedRowCount();

        TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
            getTtfFpUpdateValidateLvl2View1();
        ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
        ttfFpUpdateValidateLvl2View.executeQuery();

        if (validasiTglFpNumRow == 0) {
            TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
            ttfFpUpdateValidateLvl2Row.setTglFpVal(null);
            ttfFpUpdateValidateLvl2Row.setStatus("YCR");
            ttfFpUpdateValidateLvl2Row.setAmountPenggantian(null);
        } else {
            TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
            ttfFpUpdateValidateLvl2Row.setTglFpVal("F");
            ttfFpUpdateValidateLvl2Row.setStatus("KDL");
            ttfFpUpdateValidateLvl2Row.setAmountPenggantian(amtTax);
        }
    }

    private void validasiNilaiDpp(String keyFpBpb) {

        ValidasiNilaiDppViewImpl validasiNilaiDppView;
        validasiNilaiDppView = getValidasiNilaiDppView1();
        validasiNilaiDppView.setkeyFpBpb(keyFpBpb);
        validasiNilaiDppView.executeQuery();

        long validasiNilaiDppNumRow =
            validasiNilaiDppView.getEstimatedRowCount();

        if (validasiNilaiDppNumRow > 0) {
            ValidasiNilaiDppViewRowImpl validasiNilaiDppRow =
                (ValidasiNilaiDppViewRowImpl)validasiNilaiDppView.first();

            BigDecimal amtDppFp = new BigDecimal(validasiNilaiDppRow.getAmountDpp().toString());
            BigDecimal totalDppBpb = new BigDecimal(validasiNilaiDppRow.getTotalDpp().toString());
            BigDecimal dppTolerance = nilaiToleransiDpp();
            
            BigDecimal minAmount = totalDppBpb.subtract(dppTolerance);            

            TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
                getTtfFpUpdateValidateLvl2View1();
            ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
            ttfFpUpdateValidateLvl2View.executeQuery();
            
            if (amtDppFp.compareTo(minAmount) < 0) {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtDppVal("F");
            } else {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtDppVal(null);
            }
            
        }

    }

    private void validasiNilaiPpn(String keyFpBpb) {

        ValidasiNilaiTaxViewImpl validasiNilaiTaxView;
        validasiNilaiTaxView = getValidasiNilaiTaxView1();
        validasiNilaiTaxView.setkeyFpBpb(keyFpBpb);
        validasiNilaiTaxView.executeQuery();

        long validasiNilaiTaxNumRow =
            validasiNilaiTaxView.getEstimatedRowCount();

        if (validasiNilaiTaxNumRow > 0) {
            ValidasiNilaiTaxViewRowImpl validasiNilaiTaxRow =
                (ValidasiNilaiTaxViewRowImpl)validasiNilaiTaxView.first();

            BigDecimal amtTaxFp = new BigDecimal(validasiNilaiTaxRow.getAmountTax().toString());
            BigDecimal totalTaxBpb = new BigDecimal(validasiNilaiTaxRow.getTotalTax().toString());
            BigDecimal taxTolerance = nilaiToleransiPpn();
            
            BigDecimal minAmount = totalTaxBpb.subtract(taxTolerance);            

            TtfFpUpdateValidateLvl2ViewImpl ttfFpUpdateValidateLvl2View =
                getTtfFpUpdateValidateLvl2View1();
            ttfFpUpdateValidateLvl2View.setkeyFpBpb(keyFpBpb);
            ttfFpUpdateValidateLvl2View.executeQuery();
            
            if (amtTaxFp.compareTo(minAmount) < 0) {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtTaxVal("F");
            } else {
                TtfFpUpdateValidateLvl2ViewRowImpl ttfFpUpdateValidateLvl2Row =
                    (TtfFpUpdateValidateLvl2ViewRowImpl)ttfFpUpdateValidateLvl2View.first();
                ttfFpUpdateValidateLvl2Row.setAmtTaxVal(null);
            }
            
        }

    }

    // Main Validation

    public void startValidationLvl1(String keyTtf, String siteCode,
                                    String branchCode, String noPkp) {

        // Faktur Pajak Validation
        TtfFpValidationViewImpl ttfFpValidationView =
            getTtfFpValidationView1();
        ttfFpValidationView.setkeyTtf(keyTtf);
        ttfFpValidationView.executeQuery();

        long ttfFpNumRow = ttfFpValidationView.getEstimatedRowCount();

        if (ttfFpNumRow > 0) {
            for (int y = 0; y < ttfFpNumRow; y++) {

                TtfFpValidationViewRowImpl ttfFpValidationRow =
                    (TtfFpValidationViewRowImpl)ttfFpValidationView.getRowSetIterator().next();

                String pkpSupplier = ttfFpValidationRow.getNoPkpSup();
                String pkpIndomarco = ttfFpValidationRow.getNoPkpIdm();
                String noFp = ttfFpValidationRow.getNoFp();
                String keyFpBpb = ttfFpValidationRow.getKeyFpBpb();

                //Validate Faktur Pajak Component Level 1
                validasiPkpSup(keyFpBpb, branchCode, siteCode, pkpSupplier);
                validasiPkpIndomarco(keyFpBpb, branchCode, pkpIndomarco);
                validasiFormatFp(keyFpBpb, noFp);

            }
        }

        // BPB Validation
        TtfBpbValidationViewImpl ttfBpbValidationView =
            getTtfBpbValidationView1();
        ttfBpbValidationView.setkeyTtf(keyTtf);
        ttfBpbValidationView.executeQuery();

        long ttfBpbNumRow = ttfBpbValidationView.getEstimatedRowCount();

        if (ttfBpbNumRow > 0) {
            for (int x = 0; x < ttfBpbNumRow; x++) {

                TtfBpbValidationViewRowImpl ttfBpbValidationRow =
                    (TtfBpbValidationViewRowImpl)ttfBpbValidationView.getRowSetIterator().next();

                String noBpb = ttfBpbValidationRow.getInvoiceNum();
                String keyFpBpb = ttfBpbValidationRow.getKeyFpBpb();

                // Validate BPB Component Level 1
                validasiNoBpb(keyFpBpb, noBpb, siteCode);
            }
        }

        this.getDBTransaction().commit();
    }

    public void startValidationLvl12(String keyTtf, String siteCode,
                                     String branchCode, String noPkp) {

        // Faktur Pajak Validation
        TtfFpValidationViewImpl ttfFpValidationView =
            getTtfFpValidationView1();
        ttfFpValidationView.setkeyTtf(keyTtf);
        ttfFpValidationView.executeQuery();

        long ttfFpNumRow = ttfFpValidationView.getEstimatedRowCount();

        if (ttfFpNumRow > 0) {
            for (int y = 0; y < ttfFpNumRow; y++) {

                TtfFpValidationViewRowImpl ttfFpValidationRow =
                    (TtfFpValidationViewRowImpl)ttfFpValidationView.getRowSetIterator().next();

                String pkpSupplier = ttfFpValidationRow.getNoPkpSup();
                String pkpIndomarco = ttfFpValidationRow.getNoPkpIdm();
                String noFp = ttfFpValidationRow.getNoFp();
                String keyFpBpb = ttfFpValidationRow.getKeyFpBpb();
                oracle.jbo.domain.Number amtTax = ttfFpValidationRow.getAmountTax();
                
                // Validate Faktur Pajak Component Level 1
                validasiPkpSup(keyFpBpb, branchCode, siteCode, pkpSupplier);
                validasiPkpIndomarco(keyFpBpb, branchCode, pkpIndomarco);
                validasiFormatFp(keyFpBpb, noFp);
                
                // Validate Faktur Pajak Component Level 2
                validasiTglFp(keyFpBpb, amtTax);
                validasiNilaiDpp(keyFpBpb);
                validasiNilaiPpn(keyFpBpb);

            }
        }

        // BPB Validation
        TtfBpbValidationViewImpl ttfBpbValidationView =
            getTtfBpbValidationView1();
        ttfBpbValidationView.setkeyTtf(keyTtf);
        ttfBpbValidationView.executeQuery();

        long ttfBpbNumRow = ttfBpbValidationView.getEstimatedRowCount();

        if (ttfBpbNumRow > 0) {
            for (int x = 0; x < ttfBpbNumRow; x++) {

                TtfBpbValidationViewRowImpl ttfBpbValidationRow =
                    (TtfBpbValidationViewRowImpl)ttfBpbValidationView.getRowSetIterator().next();

                String noBpb = ttfBpbValidationRow.getInvoiceNum();
                String keyFpBpb = ttfBpbValidationRow.getKeyFpBpb();
                
                // Validate BPB Component Level 1
                validasiNoBpb(keyFpBpb, noBpb, siteCode);

                // Validate BPB Component Level 2
                validasiTglBpb(keyTtf, noBpb);

            }
        }

        this.getDBTransaction().commit();
    }

    private BigDecimal nilaiToleransiDpp() {        
       BigDecimal toleransiDpp = BigDecimal.ZERO;

        NilaiToleransiDppViewImpl nilaiToleransiDppView =
            getNilaiToleransiDppView1();
        nilaiToleransiDppView.executeQuery();

        long nilaiToleransiDppNumRow =
            nilaiToleransiDppView.getEstimatedRowCount();

        if (nilaiToleransiDppNumRow > 0) {
            NilaiToleransiDppViewRowImpl nilaiToleransiDppViewRow = 
                (NilaiToleransiDppViewRowImpl) nilaiToleransiDppView.first();
            
            toleransiDpp = new BigDecimal(nilaiToleransiDppViewRow.getParamValue());
        } else {
            System.out.println("------------------------------------------------------");
            System.out.println("ERROR: Konfigurasi parameter toleransi tidak ditemukan");
            System.out.println("------------------------------------------------------");
        }

        return toleransiDpp;
    }

    private BigDecimal nilaiToleransiPpn() {        
        BigDecimal toleransiPpn = BigDecimal.ZERO;

        NilaiToleransiPpnViewImpl nilaiToleransiPpnView =
            getNilaiToleransiPpnView1();
        nilaiToleransiPpnView.executeQuery();

        long nilaiToleransiPpnNumRow =
            nilaiToleransiPpnView.getEstimatedRowCount();

        if (nilaiToleransiPpnNumRow > 0) {
            NilaiToleransiPpnViewRowImpl nilaiToleransiPpnViewRow = 
                (NilaiToleransiPpnViewRowImpl) nilaiToleransiPpnView.first();
            
            toleransiPpn = new BigDecimal(nilaiToleransiPpnViewRow.getParamValue());
        } else {
            System.out.println("------------------------------------------------------");
            System.out.println("ERROR: Konfigurasi parameter toleransi tidak ditemukan");
            System.out.println("------------------------------------------------------");
        }

        return toleransiPpn;
    }

    /**
     * Container's getter for ValidasiNpwpIndomarcoView1.
     * @return ValidasiNpwpIndomarcoView1
     */
    public ValidasiNpwpIndomarcoViewImpl getValidasiNpwpIndomarcoView1() {
        return (ValidasiNpwpIndomarcoViewImpl)findViewObject("ValidasiNpwpIndomarcoView1");
    }

    /**
     * Container's getter for ValidasiNpwpSupplierView1.
     * @return ValidasiNpwpSupplierView1
     */
    public ValidasiNpwpSupplierViewImpl getValidasiNpwpSupplierView1() {
        return (ValidasiNpwpSupplierViewImpl)findViewObject("ValidasiNpwpSupplierView1");
    }

    /**
     * Container's getter for TtfBpbValidationView1.
     * @return TtfBpbValidationView1
     */
    public TtfBpbValidationViewImpl getTtfBpbValidationView1() {
        return (TtfBpbValidationViewImpl)findViewObject("TtfBpbValidationView1");
    }

    /**
     * Container's getter for TtfFpValidationView1.
     * @return TtfFpValidationView1
     */
    public TtfFpValidationViewImpl getTtfFpValidationView1() {
        return (TtfFpValidationViewImpl)findViewObject("TtfFpValidationView1");
    }

    /**
     * Container's getter for TtfFpUpdateValidateView1.
     * @return TtfFpUpdateValidateView1
     */
    public TtfFpUpdateValidateViewImpl getTtfFpUpdateValidateView1() {
        return (TtfFpUpdateValidateViewImpl)findViewObject("TtfFpUpdateValidateView1");
    }

    /**
     * Container's getter for ValidasiNoBpbView1.
     * @return ValidasiNoBpbView1
     */
    public ValidasiNoBpbViewImpl getValidasiNoBpbView1() {
        return (ValidasiNoBpbViewImpl)findViewObject("ValidasiNoBpbView1");
    }

    /**
     * Container's getter for TtfBpbUpdateValidateView1.
     * @return TtfBpbUpdateValidateView1
     */
    public TtfBpbUpdateValidateViewImpl getTtfBpbUpdateValidateView1() {
        return (TtfBpbUpdateValidateViewImpl)findViewObject("TtfBpbUpdateValidateView1");
    }

    /**
     * Container's getter for TtfBpbUpdateDataView1.
     * @return TtfBpbUpdateDataView1
     */
    public ViewObjectImpl getTtfBpbUpdateDataView1() {
        return (ViewObjectImpl)findViewObject("TtfBpbUpdateDataView1");
    }

    /**
     * Container's getter for TtfFpUpdateDataView1.
     * @return TtfFpUpdateDataView1
     */
    public TtfFpUpdateDataViewImpl getTtfFpUpdateDataView1() {
        return (TtfFpUpdateDataViewImpl)findViewObject("TtfFpUpdateDataView1");
    }

    /**
     * Container's getter for ValidasiTglBpbView1.
     * @return ValidasiTglBpbView1
     */
    public ValidasiTglBpbViewImpl getValidasiTglBpbView1() {
        return (ValidasiTglBpbViewImpl)findViewObject("ValidasiTglBpbView1");
    }

    /**
     * Container's getter for TtfBpbUpdateValidateLvl2View1.
     * @return TtfBpbUpdateValidateLvl2View1
     */
    public TtfBpbUpdateValidateLvl2ViewImpl getTtfBpbUpdateValidateLvl2View1() {
        return (TtfBpbUpdateValidateLvl2ViewImpl)findViewObject("TtfBpbUpdateValidateLvl2View1");
    }

    /**
     * Container's getter for ValidasiTglFpView1.
     * @return ValidasiTglFpView1
     */
    public ValidasiTglFpViewImpl getValidasiTglFpView1() {
        return (ValidasiTglFpViewImpl)findViewObject("ValidasiTglFpView1");
    }

    /**
     * Container's getter for TtfFpUpdateValidateLvl2View1.
     * @return TtfFpUpdateValidateLvl2View1
     */
    public TtfFpUpdateValidateLvl2ViewImpl getTtfFpUpdateValidateLvl2View1() {
        return (TtfFpUpdateValidateLvl2ViewImpl)findViewObject("TtfFpUpdateValidateLvl2View1");
    }

    /**
     * Container's getter for ValidasiNilaiDppView1.
     * @return ValidasiNilaiDppView1
     */
    public ValidasiNilaiDppViewImpl getValidasiNilaiDppView1() {
        return (ValidasiNilaiDppViewImpl)findViewObject("ValidasiNilaiDppView1");
    }

    /**
     * Container's getter for ValidasiNilaiTaxView1.
     * @return ValidasiNilaiTaxView1
     */
    public ValidasiNilaiTaxViewImpl getValidasiNilaiTaxView1() {
        return (ValidasiNilaiTaxViewImpl)findViewObject("ValidasiNilaiTaxView1");
    }

    /**
     * Container's getter for NilaiToleransiDppView1.
     * @return NilaiToleransiDppView1
     */
    public NilaiToleransiDppViewImpl getNilaiToleransiDppView1() {
        return (NilaiToleransiDppViewImpl)findViewObject("NilaiToleransiDppView1");
    }

    /**
     * Container's getter for NilaiToleransiPpnView1.
     * @return NilaiToleransiPpnView1
     */
    public NilaiToleransiPpnViewImpl getNilaiToleransiPpnView1() {
        return (NilaiToleransiPpnViewImpl)findViewObject("NilaiToleransiPpnView1");
    }

    /**
     * Container's getter for CheckValLevel1CompleteView1.
     * @return CheckValLevel1CompleteView1
     */
    public CheckValLevel1CompleteViewImpl getCheckValLevel1CompleteView1() {
        return (CheckValLevel1CompleteViewImpl)findViewObject("CheckValLevel1CompleteView1");
    }

    /**
     * Container's getter for CheckValLevel2CompleteView1.
     * @return CheckValLevel2CompleteView1
     */
    public CheckValLevel2CompleteViewImpl getCheckValLevel2CompleteView1() {
        return (CheckValLevel2CompleteViewImpl)findViewObject("CheckValLevel2CompleteView1");
    }

    /**
     * Container's getter for TtfHeaderMemoMdView1.
     * @return TtfHeaderMemoMdView1
     */
    public ViewObjectImpl getTtfHeaderMemoMdView1() {
        return (ViewObjectImpl)findViewObject("TtfHeaderMemoMdView1");
    }

    /**
     * Container's getter for CheckNilaiDppTidakSamaView1.
     * @return CheckNilaiDppTidakSamaView1
     */
    public ViewObjectImpl getCheckNilaiDppTidakSamaView1() {
        return (ViewObjectImpl)findViewObject("CheckNilaiDppTidakSamaView1");
    }

    /**
     * Container's getter for CheckNilaiTaxTidakSamaView1.
     * @return CheckNilaiTaxTidakSamaView1
     */
    public ViewObjectImpl getCheckNilaiTaxTidakSamaView1() {
        return (ViewObjectImpl)findViewObject("CheckNilaiTaxTidakSamaView1");
    }

    /**
     * Container's getter for CheckTglBpbExpiredView1.
     * @return CheckTglBpbExpiredView1
     */
    public ViewObjectImpl getCheckTglBpbExpiredView1() {
        return (ViewObjectImpl)findViewObject("CheckTglBpbExpiredView1");
    }

    /**
     * Container's getter for CheckTglFpExpiredView1.
     * @return CheckTglFpExpiredView1
     */
    public ViewObjectImpl getCheckTglFpExpiredView1() {
        return (ViewObjectImpl)findViewObject("CheckTglFpExpiredView1");
    }

    /**
     * Container's getter for TtfHeaderUpdateStatusView1.
     * @return TtfHeaderUpdateStatusView1
     */
    public TtfHeaderUpdateStatusViewImpl getTtfHeaderUpdateStatusView1() {
        return (TtfHeaderUpdateStatusViewImpl)findViewObject("TtfHeaderUpdateStatusView1");
    }
}
